/**
 * 
 */
package com.jinmei.web.ws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.map.MultiValueMap;

import com.jinmei.model.Employee;
import com.jinmei.model.WorkSchedule;
import com.jinmei.utils.JinmeiConsts;
import com.jinmei.web.WorkScheduleDayDTO;

/**
 * Parse dtos to work schedules information, 
 * in the day we selected.
 * 
 * @author richard
 *
 */
public class WorkScheduleEntityParser {
	
	/**
	 *  data transfer object from jsp
	 */
	private List<WorkScheduleDayDTO> dtos;
	/**
	 * Work date from calendar day
	 */
	private Date workDate;
	
	/**
	 *  all work schedules by work date in DB.
	 */
	private List<WorkSchedule> oldWSByDate = new ArrayList<WorkSchedule>(); 
	
	/**
	 *  all work schedules by work date from jsp.
	 */
	private List<WorkSchedule> newWSByDate = new ArrayList<WorkSchedule>(); 
	
	/**
	 * used for notification.
	 * work schedules info by workdate at employee(map key) level in DB 
	 */
	private MultiValueMap oldMultiWSMapByEmployeeId = new MultiValueMap();
	
	/**
	 * used for notification.
	 * work schedules info by workdate at employee(map key) level from jsp
	 */
	private MultiValueMap newMultiWSMapByEmployeeId = new MultiValueMap();
	
	public MultiValueMap getOldMultiWSMapByEmployeeId() {
		return oldMultiWSMapByEmployeeId;
	}

	public MultiValueMap getNewMultiWSMapByEmployeeId() {
		return newMultiWSMapByEmployeeId;
	}

	public List<WorkSchedule> getNewWSByDate() {
		return newWSByDate;
	}

	public WorkScheduleEntityParser(List<WorkScheduleDayDTO> dtos, 
			List<WorkSchedule> oldWSByDate, Date workDate) {
		this.dtos = dtos;
		this.oldWSByDate = oldWSByDate;
		this.workDate = workDate;
		parse();
	}
	
	public void parse() {
		if (dtos == null || dtos.size() == 0) return;
		oldMultiWSMapByEmployeeId = createMultiWSMapByEmployeeId(oldWSByDate);
		
		WorkSchedule newWorkSchedule = null;
		for (WorkScheduleDayDTO dto : dtos) {
			if (dto.getIsWorkingTypeA() != null && dto.getIsWorkingTypeA()) {
				// new schedule of type A
				newWorkSchedule = new WorkSchedule();
				newWorkSchedule.setEmployee(new Employee(dto.getFid()));
				newWorkSchedule.setWorkDate(workDate);
				newWorkSchedule.setWorkingType(JinmeiConsts.WORKING_TYPE_A);
				newMultiWSMapByEmployeeId.put(dto.getFid(), newWorkSchedule);
				newWSByDate.add(newWorkSchedule);
			}
			
			if (dto.getIsWorkingTypeB() != null && dto.getIsWorkingTypeB()) {
				// new schedule of type B
				newWorkSchedule = new WorkSchedule();
				newWorkSchedule.setEmployee(new Employee(dto.getFid()));
				newWorkSchedule.setWorkDate(workDate);
				newWorkSchedule.setWorkingType(JinmeiConsts.WORKING_TYPE_B);
				newMultiWSMapByEmployeeId.put(dto.getFid(), newWorkSchedule);
				newWSByDate.add(newWorkSchedule);
			}
			
			if (dto.getIsWorkingTypeC() != null && dto.getIsWorkingTypeC()) {
				// new schedule of type C
				newWorkSchedule = new WorkSchedule();
				newWorkSchedule.setEmployee(new Employee(dto.getFid()));
				newWorkSchedule.setWorkDate(workDate);
				newWorkSchedule.setWorkingType(JinmeiConsts.WORKING_TYPE_C);
				newMultiWSMapByEmployeeId.put(dto.getFid(), newWorkSchedule);
				newWSByDate.add(newWorkSchedule);
			}
			
			if (dto.getIsNotOnDuty() != null && dto.getIsNotOnDuty()) {
				// new schedule of not on duty
				newWorkSchedule = new WorkSchedule();
				newWorkSchedule.setEmployee(new Employee(dto.getFid()));
				newWorkSchedule.setWorkDate(workDate);
				newWorkSchedule.setWorkingType(JinmeiConsts.WORKING_TYPE_NOTONDUTY);
				newMultiWSMapByEmployeeId.put(dto.getFid(), newWorkSchedule);
				newWSByDate.add(newWorkSchedule);
			}
		}

	}
	
	private static MultiValueMap createMultiWSMapByEmployeeId(List<WorkSchedule> source) {
		if (source == null) return new MultiValueMap();
		MultiValueMap map = new MultiValueMap();
		
		for (WorkSchedule workSchedule : source) {
			map.put(workSchedule.getEmployee().getfId(), workSchedule);
		}
		return map;
	}
}
